﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class LayDuctAccessory : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Result result;
			try
			{
				if (commandData == null)
				{
					throw new System.ArgumentNullException();
				}
				UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
				Document document = activeUIDocument.Document;
				Selection selection = activeUIDocument.Selection;
				if (!YJKRevitTools.ViewCheck(activeUIDocument, new List<ViewType>
				{
					1,
					2,
					4,
					3,
					117
				}))
				{
					result = Autodesk.Revit.UI.Result.Cancelled;
				}
				else
				{
					HYEquipmentCommon hyequipmentCommon = new HYEquipmentCommon();
					ICollection<ElementId> allFamilyInstance = hyequipmentCommon.GetAllFamilyInstance(document, BuiltInCategory.OST_DuctAccessory);
					for (;;)
					{
						try
						{
							FamilySymbol familySymbol = new ILayForm(document).LayDuctAccessoryFormShow(document, "风管阀件");
							if (familySymbol != null)
							{
								if (familySymbol != null)
								{
									activeUIDocument.PromptForFamilyInstancePlacement(familySymbol);
								}
								continue;
							}
						}
						catch (Autodesk.Revit.Exceptions.OperationCanceledException)
						{
							continue;
						}
						catch (Exception ex)
						{
							message = ex.Message;
							return Autodesk.Revit.UI.Result.Failed;
						}
						break;
					}
					foreach (ElementId elementId in hyequipmentCommon.GetAllFamilyInstance(document, BuiltInCategory.OST_DuctAccessory))
					{
						foreach (ElementId elementId2 in allFamilyInstance)
						{
							if (elementId == elementId2)
							{
								break;
							}
						}
					}
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex2)
			{
				message = ex2.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}
	}
}
